<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div></div>
    <script src="./xhr.js"></script>
    <script>
        let isObj = function(obj){
            return typeof obj == 'object' && obj != null;
        }

        let flatObj = function(data){
            target = {}
            let setItem = function(data,name){
                Object.keys(data).forEach(function(key,i){
                    //将key值存储到target对象中
                    let value = data[key];
                    if(isObj(value)){
                        key = !name && key || name+'['+key+']';
                        setItem(value,key);
                    }else{
                        key = !name && key || name+'['+key+']';
                        target[key] = value
                    }
                })
            };
            setItem(data);
            return target;
        }

        let data = {
            schools:'国防'
            ,user:{name:['ck','陈康'],age:18,great:{'one':98,'two':100}}
            ,is:false
        }

        let flat = flatObj(data)

        console.log(flat)

        xhr({
            url:'https://www.imooc.com/search/hotwords'
            ,method:'post'
            ,dataType: 'json' // 表示服务端响应的数据类型
            ,data:{
                schools:'国防'
                ,user:{name:['ck','陈康'],age:18,great:{'one':98,'two':[100,101]}}
                ,is:false
            }
        }).then(function(data) {
            console.log('data:', data)
        })

    </script>
</body>
</html>